-
Notifications
You must be signed in to change notification settings - Fork 1.1k
bench: replace wall-clock timer with per-process CPU timer #1732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Just some quick comments:
I think there's a reason to have this. Some benchmarks take much longer than others, so it probably makes sense to run fewer iters for these.
I think
Well, okay, that has a history; see #689. It's debatable if it makes sense to avoid floating point math, but as long as it doesn't get in your way here, it's a cool thing to keep it. :D |
It will be useful to split your changes into meaningful and separate commits, see https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#committing-patches. |
I think |
If we're going to rework this, I'd suggest using the stabilized quartiles approach from https://cr.yp.to/papers/rsrst-20250727.pdf:
|
6aff035
to
d456fad
Compare
right now all benchmarks are run with count=10 and fixed iters (apart from ecmult_multi which adjusts the number of iters, not count). therefore |
I disagree with #689. It overcomplicate things for the sake of not having floating point math. those divisions aren't even in the hot path, they're outside the benchmarks. |
Concept NACK on removing any ability to observe variance in timing. The current min/avg/max are far from perfect, but they work fairly well in practice. Improving is welcome, but removing them is a step backwards. |
what is the usefulness of measuring min/max when we are removing OS interference & thermal throttling out of the equation? min/max will be extremely close to the avg no matter how bad the benchmarked function is. |
97e5264
to
254a014
Compare
1d9d6d0
to
4c9a074
Compare
by the way, |
ddeaede
to
71dff3f
Compare
even though the manual says that I added a line in the README.md for best practices to run the benchmarks. I also tried adding a function to pin the process to a core directly in C, but there's no standard POSIX compliant way to do so. There is |
3e43c75
to
ef9e40e
Compare
The point is exactly having a simple way of verifying that there's indeed no interference. Getting rid of sources of variance is hard to get right, and it's impossible to get a perfect solution. (This discussion shows this!) So we better have a way of spotting if something is off. I like the stabilized quartiles idea. |
tbh it scares me a bit, will see what I can do. Maybe in a future PR. |
ef9e40e
to
66745f7
Compare
I haven't investigated this in detail but couldn't find any related issue: were there any efforts to use https://nanobench.ankerl.com like we do in Core instead? That will likely need some adjustments on the benchmarks themselves, but at least we wouldn't be rolling our own framework. |
not familiar with nano bench, but I can see it uses also the user would need to have a c++ compiler, not a big real but I think this repo needs to strictly adhere to c89. |
I realized the windows version I implemented was using a unprecise clock. drafting. will fix. |
8242f55
to
6ec4255
Compare
I changed windows clock to |
nanobench is a framework for benchmarking C++ but this is C. Of course, we could still call our C from C++, but adding C++ to the code base seems a bit overkill to me if if's "just" for the purpose of benchmarks. I agree that using an existing framework may be a good idea, but I'm not aware of any in pure C. |
The purpose would be to standardize the benchmarking to avoid reinventing solutions to problems that aren't strictly related to the purpose of this library. It would also help with trusting the result more, since we already know how that benchmarking library behaves. |
Sorry, I didn't want to be discouraging. I don't think at all that nanobench is a far-fetched idea. And I truly agree that reinventing the wheel is not great. In the end, what counts is the maintenance burden. My main concern with C++ is that I'm not sure how fluent our regular contributors are in C++. (I'm not, but luckily I'm not the only one here.) If the bit of C++ is harder to touch (for us) than this code, then nanobench is not a good idea. If, on the other hand, we have people who can maintain C++, or if we get support from the Core contributors who are familiar with nanobench, then this can be a win. I'd be happy to see a demo/WIP PR but if you feel that this is a lot of work, then it might be a good idea to see what other contributors and maintainers think. |
This PR currently conflicts with #1734 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK mod nits, still need to test this
3e58a91
to
8925b95
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to https://www.man7.org/linux/man-pages/man3/clock_gettime.3.html:
Link with
-lrt
(only for glibc versions before 2.17).
I think this check, and adding the -lrt
flag if necessary, should be included in both build systems.
src/bench.c
Outdated
* file COPYING or https://www.opensource.org/licenses/mit-license.php.* | ||
***********************************************************************/ | ||
|
||
#define _POSIX_C_SOURCE 200112L /* for clock_gettime() */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any references explaining why the value 200112L
was chosen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no particular reason besides the fact that it is the bare minimum version to make it work on my machine and CI.
The reported 199309L
version number didn't expose the necessary macros.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reported
199309L
version number didn't expose the necessary macros.
I can't reproduce this on my machine.
no particular reason besides the fact that it is the bare minimum version to make it work on my machine...
Could you share your build environment details so I can try to reproduce the issue?
... and CI.
It seems that CI is fine with the documented 199309L
value.
See: https://github.com/hebasto/secp256k1/actions/runs/18008380590
Agreed, good catch! what do you think about something like this? include(CheckFunctionExists)
check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
if(NOT HAVE_CLOCK_GETTIME)
# On some platforms, clock_gettime requires librt
target_link_libraries(your_target PRIVATE rt)
endif() |
Friendly ping @martinus, benchmark connoisseur :) Could you please give a rough estimate of these changes at the concept level? |
And what's your take on using nanobench instead, even though this is a pure C library instead of C++? |
I've tested 8925b95 by running it alongside Could you please clarify how one would measure or observe the claimed "less influenced by the environment" behaviour? |
a better example would be starting various threads with random usage spikes at random times on the same core the benchmark is running on. basically, if you have no other processes running on that CPU, then this PR won't show any improvement. this PR helps in the more realistic scenario where a user has background processes running, and the scheduler assigns multiple of them on the same CPU that's running the benchmark, putting the thread to sleep, which the wall clock timer doesn't account for. also, this PR is not merely a replacement of wall clock time with CPU time, but it also modernizes the clock function as per the Unix standard. |
Feel free to grab f59c45f from https://github.com/hebasto/secp256k1/commits/pr1732/0925.cmake. UPD. @real-or-random do we need a CI job to build on some old system with old glibc? |
static void print_clock_info(void) { | ||
#if defined(CLOCK_PROCESS_CPUTIME_ID) | ||
printf("INFO: Using per-process CPU timer\n\n"); | ||
#else | ||
printf("WARN: Using wall-clock timer instead of per-process CPU timer.\n\n"); | ||
#endif | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these messages make sense on Windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while on windows there's no option for per-process cpu clock (or at least not high precision), I still think issuing the warning is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems a bit of a stretch to call the native Windows QPC framework a "wall-clock timer".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about this?
"WARN: Using global timer instead of per-process CPU timer."
This commit improves the reliability of benchmarks by removing some of the influence of other background running processes. This is achieved by using CPU bound clocks that aren't influenced by interrupts, sleeps, blocked I/O, etc.
This seems overcomplicated and convoluted. but if you manage to simplify it I'll include your commit. |
8925b95
to
cce0147
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach NACK cce0147.
The current implementation breaks compatibility with systems using glibc
versions prior to 2.17:
$ ldd --version | head -1
ldd (Ubuntu EGLIBC 2.15-0ubuntu10.23) 2.15
$ cmake -B build
$ cmake --build build -t bench
<snip>
[100%] Linking C executable ../bin/bench
CMakeFiles/bench.dir/bench.c.o: In function `gettime_us':
/secp256k1/src/bench.h:45: undefined reference to `clock_gettime'
collect2: ld returned 1 exit status
make[3]: *** [bin/bench] Error 1
make[2]: *** [src/CMakeFiles/bench.dir/all] Error 2
make[1]: *** [src/CMakeFiles/bench.dir/rule] Error 2
make: *** [bench] Error 2
Goal
This PR refactors the benchmarking functions as per #1701, in order to make benchmarks more deterministic and less influenced by the environvment.
This is achieved by replacing Wall-Clock Timer with Per-Process CPU Timer when possible.